package com.example.wordexport;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenbin
 * @date 2021/10/11
 */
public class WrodDemo {


    @Test
    public void imageWordExport() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("department", "Easypoi");
        map.put("person", "JueYue");
        map.put("time", "234234");
        ImageEntity image = new ImageEntity();
        image.setHeight(200);
        image.setWidth(500);
        image.setUrl("C:\\Users\\chenbin\\Pictures\\1.jpg");
        image.setType(ImageEntity.URL);
        map.put("img", image);
        List<Map<String, String>> list = new ArrayList<>();
        for(int i = 0;i<10;i++) {
            Map<String,String> map1 = new HashMap<>();

            map1.put("name","xiao");
            map1.put("age","12");

            list.add(map1);
        }

        map.put("list",list);
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "E:\\IdeaProjects\\spring-cloud-alibaba-demo\\img-to-word\\src\\main\\resources\\export.docx", map);
            FileOutputStream fos = new FileOutputStream("D:/image.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**

     * 简单导出没有图片和Excel

     */
    @Test
    public void SimpleWordExport() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("department", "Easypoi");
        map.put("person", "JueYue");
        map.put("time", "");
        map.put("me","JueYue");
        map.put("date", "2015-01-03");

        List<Map<String, String>> list = new ArrayList<>();
        for(int i = 0;i<10;i++) {
            Map<String,String> map1 = new HashMap<>();

            map1.put("name","xiao");
            map1.put("age","12");

            list.add(map1);
        }

        map.put("list",list);
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(
                    "cn/afterturn/easypoi/test/word/doc/Simple.docx", map);
            FileOutputStream fos = new FileOutputStream("D:/excel/simple.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
